<template>
  <div class="zone-manage">
    <div class="manage-header">
      <el-button type="primary" size="small" @click="addItem">新增</el-button>
      <div class="search-box">
        <el-input class="keyword-input" size="small" :clearable="true"
                  v-model="searchKeyword" placeholder="请输入地块名称"></el-input>
        <el-button type="primary" size="small" @click="loadList()">搜索</el-button>
      </div>
    </div>
    <el-table
        :data="zoneList"
        style="width: 100%"
        size="small"
        :row-style="{height: '20px'}"
        :cell-style="{padding: '8px', 'text-align': 'center'}"
        :header-cell-style="{ background: '#dedede', color: '#606266','text-align':'center'  }"

    >
      <el-table-column prop="name" label="地块名称" width="120"></el-table-column>
      <el-table-column prop="type" label="类型" width="100"></el-table-column>
      <el-table-column prop="size" label="大小(亩)"></el-table-column>
      <el-table-column prop="intro" label="说明"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column prop="nickname" label="创建用户" width="100"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160"></el-table-column>
      <el-table-column prop="nickname" label="修改用户" width="100"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="160"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="small" type="primary"
                     @click="editItem(scope.row)">修改
          </el-button>
          <el-button size="small" type="danger"
                     @click="deleteItem(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-container">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="page.currentPage"
          :page-size="page.ageSize"
          :total="page.total"
          layout="total, prev, pager, next"
      >
      </el-pagination>
    </div>

    <!-- 新增、编辑 的对话框 -->
    <el-dialog title="新增地块类型" :visible.sync="dialogVisible" >
      <el-form :model="zoneForm" :rules="rules" ref="zoneForm" class="zone-form">
        <el-row>
          <el-col :span="20">
            <div>
              <el-form-item label="地块名称" label-width="100px" prop="name">
                <el-input v-model="zoneForm.name" autocomplete="off"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地块类型" label-width="100px" prop="type">
              <el-input v-model="zoneForm.type" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地块大小(亩)" label-width="120px" prop="size" >
              <el-input v-model="zoneForm.size" autocomplete="off" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地块说明" label-width="100px" prop="intro" >
              <el-input type="textarea" v-model="zoneForm.intro"
                        maxlength="300"
                        show-word-limit
                        :rows="3" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态" label-width="100px" >
              <el-switch v-model="zoneForm.status"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import {getZoneList, addZone, deleteZone, updateZone} from "@/api/zone";

export default {
  name: "zone",
  data() {
    return {
      addOrEdit: 'add',
      zoneList: [],
      dialogVisible: false,
      searchKeyword: '',
      zoneForm: {
        name: '',
        type: '',
        size: '',
        intro: '',
        status:true,
      },
      rules: {
        name: [
          {required: true, message: '请填写地块名称', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请填写地块类型', trigger: 'blur'}
        ],
        size: [
          {required: true, message: '请填写地块大小', trigger: 'blur'}
        ],
        intro: [
          {required: true, message: '请填写描述信息', trigger: 'blur'},
          {max: 200, message: '长度不超过 200 个字符', trigger: 'change'}
        ]
      },
      //分页相关
      page: {
        currentPage: 1,
        total: 0,
        pageSize: 10
      }


    }
  }, mounted() {
    this.loadList();
  },created() {

  },
  methods: {
    loadList(pageNum, pageSize) {
      if (!pageNum) {
        pageNum = 1
      }
      if (!pageSize) {
        pageSize = 10
      }
      const params = {
        zoneName:''
      }
      if(this.searchKeyword){
        params.zoneName = this.searchKeyword
      }
      getZoneList(pageNum, pageSize,params).then(res => {
        console.log(params)
        const {data} = res;
        if (data.code === 200) {
          this.zoneList = data.data.records;
          this.page.total = data.data.total
          this.page.currentPage = data.data.current
          this.page.pageSize = data.data.size
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleCurrentChange(current) {
      this.page.currentPage = current
      this.loadList(current, this.page.pageSize)
    },
    handleSizeChange() {

    },
    addItem() {
      this.addOrEdit = 'add'
      this.dialogVisible = true;
      this.zoneForm = {
        id: 0,
        name: '',
        type: '',
        size: '',
        intro: '',
        status:true,
      }
    },
    editItem(row) {
      this.addOrEdit = 'edit'
      this.dialogVisible = true
      this.zoneForm = {
        id: row.id,
        name: row.name,
        type: row.type,
        size: row.size,
        intro: row.intro,
        status:row.status === 1? true:false
      }

    },
    save() {
      //先对表单验证
      this.$refs.zoneForm.validate((valid) => {
        if (!valid) return;
        //调整status的类型，后端接收Int型
        this.zoneForm.status = this.zoneForm.status?1:0;
        console.log(this.zoneForm)
        if (this.addOrEdit === 'add') {
          addZone(this.zoneForm).then(res => {
            const {data} = res;
            if (data.code === 200) {
              this.dialogVisible = false;
              this.$message.success(data.msg)
              this.loadList();
            } else {
              this.$message.success(data.msg)
            }
          });
        } else {
          updateZone(this.zoneForm).then(res => {
            const {data} = res;
            if (data.code === 200) {
              this.dialogVisible = false;
              this.$message.success(data.msg)
              this.loadList();
            } else {
              this.$message.success(data.msg)
            }
          });
        }
      })


    },
    deleteItem(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteZone(id).then(res => {
          const {data} = res;
          if (data.code === 200) {
            this.$message({
              type: 'success',
              message: data.msg
            });
            this.loadList();
          } else {
            this.$message({
              type: 'warning',
              message: data.msg
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    }
  }
}
</script>

<style scoped lang="less">
.zone-manage {

  .manage-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0px 10px 0px;

    .search-box {
      display: flex;

      .keyword-input {
        margin-right: 5px;
      }
    }
  }

  .page-container {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 50px;
  }
}

.zone-form{
  padding: 20px;
}

</style>